<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>




<div id="openWin" class="easyui-window" inline="true" closed="true" iconCls="icon-save" style="width:500px;height:180px;padding:5px;">
	<div class="easyui-layout" fit="true">
		<div region="center" border="false" style="padding:10px;background:#fff;border:1px solid #ccc;">
			
			<input type="hidden" name="intId" id="intId" />
			<s:hidden name="query_contractId"/>
			<table>
				<tr>
					<td style="vertical-align: top;"><label>付款方式</label></td>
					<td colspan="3">
						<s:select id="contractPaymentId" name="contractPaymentId" list="{}"  theme="simple" onchange="paymentChange(this);" style="width: 125px;" />
						<input type="text" name="paymentRemark" id="paymentRemark"  style="display: none; width: 120px;" />
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>付款金额</label></td>
					<td colspan="3">
						<input type="text" name="paymentAmount" id="paymentAmount" class="easyui-validatebox" required="true" style="width: 120px;"  />
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>是否提供发票</label></td>
					<td>
						<s:select id="invoice" name="invoice" list="#{0:'不提供', 1:'提供'}" onchange="showInvoiceAmount(this.value);" theme="simple" style="width: 125px;" />
					</td>
					<td style="vertical-align: top;"><label id="invoiceAmountLabel" style="display: none;">发票金额</label></td>
					<td>
						<input type="text" name="invoiceAmount" id="invoiceAmount" style="display: none; width: 120px;" />
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>申请日期</label></td>
					<td>
						<input type="text" style="width: 125px;" id="requestTime" name="requestTime" />
					</td>
					<td style="vertical-align: top;"><label>付款日期</label></td>
					<td>
						<input type="text" style="width: 125px;" id="paymentTime" name="paymentTime" />
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>创建人</label></td>
					<td colspan="3">
						<s:select id="handleUserId" name="handleUserId" list="#request.employeeInfoList" listKey="employeeId" listValue="employeeName" theme="simple" style="width: 125px;" />
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>备注</label></td>
					<td colspan="3">
						<textarea rows="3" cols="35" name="remark" id="remark" ></textarea>
					</td>
				</tr>
			</table>
		</div>
		<div region="south" border="false" style="text-align:right;padding:5px 0;">
			<a class="easyui-linkbutton" iconCls="icon-ok" href="javascript:void(0)" id="btn-save" onclick="save();">保存</a>
			<a class="easyui-linkbutton" iconCls="icon-cancel" href="javascript:void(0)" onclick="$('#openWin').window('close');">关闭</a>
		</div>
	</div>
</div>
	
	
<script>

	$(function() {

		initDatePicker("paymentTime");
		initDatePicker("requestTime");

		if ($("#query_contractId").val() > 0) {
			getContractPayment($("#query_contractId").val());
			openAddForm();
		}

	});

	function save() {
		if ($('#paymentAmount').validatebox('isValid')
				&& checkAmount($('#paymentAmount').val())) {
			$("#queryForm").submit();
		}
	}

	function openAddForm() {
		$("#intId").val("");

		openWin('新增付款', 'payment_addPaymentDetailAction.action');
	}

	function openWin(title, action) {
		$("#queryForm").attr("action", action);
		
		winInit();
		
		//稍作延迟显示
		setTimeout(function() {
			$('#openWin').window({		
				title : title,
				modal : true,
				shadow : false,
				closed : false,
				collapsible : false,
				minimizable : false,
				maximizable : false,
				width : 450,
				height : 320
			});
		}, 100);
	}

	function openModifyForm() {

		var rows = $('#dataTable').datagrid('getSelections');
		if (rows.length <= 0) {
			$.messager.alert('信息','请选择一条记录！','info');
			return;
		} else if (rows.length > 1) {
			$.messager.alert('信息','只能选择一条记录！','info');
			return;
		}
		
		// 清空query_contractId，不在自动弹出付款窗口
		$("#query_contractId").val("");

		showData(rows[0].intId);
		
		openWin('修改付款', 'payment_modifyPaymentDetailAction.action');
	}

	function showData(selectId) {

		var contractId;
		var cpId;

		var url = "payment_getPaymentDetailJsonAction.action?intId=" + selectId;
		$.getJSON(url, function(data) {

			if (data != null) {
				$("#paymentAmount").val(data.paymentDetailJson.paymentAmount);
				$("#invoice").val(data.paymentDetailJson.invoice);
				$("#invoiceAmount").val(data.paymentDetailJson.invoiceAmount);
				
				var paymentTime;
				  if (data.paymentDetailJson.paymentTime != null
						  && data.paymentDetailJson.paymentTime != '') {
					  paymentTime = new Date(data.paymentDetailJson.paymentTime).format("yyyy-MM-dd");
				  } else {
					  paymentTime = "";
				  }
				$("#paymentTime").datebox('setValue', paymentTime);
				
				var requestTime;
				  if (data.paymentDetailJson.requestTime != null
						  && data.paymentDetailJson.requestTime != '') {
					  requestTime = new Date(data.paymentDetailJson.requestTime).format("yyyy-MM-dd");
				  } else {
					  requestTime = "";
				  }
				$("#requestTime").datebox('setValue', requestTime);
				
				$("#handleUserId").val(data.paymentDetailJson.handleUserId);
				$("#remark").val(data.paymentDetailJson.remark);
				$("#intId").val(data.paymentDetailJson.intId);

				contractId = data.paymentDetailJson.contractId;
				cpId = data.paymentDetailJson.contractPaymentId;

				// set invoice select
				showInvoiceAmount($("#invoice").val());
			}
		});

		// 延迟一下取数据
		setTimeout(function() {
			
			if (contractId != null && cpId != null) {
				// get Contract Payment List and set contractPayment select
				getContractPayment(contractId, cpId);
			}
		}, 1000);
		
	}

	function showInvoiceAmount(value) {
		if (value == 0) {
			$("#invoiceAmountLabel").css("display", "none");
			$("#invoiceAmount").css("display", "none");
		} else {
			$("#invoiceAmountLabel").css("display", "");
			$("#invoiceAmount").css("display", "");
		}
	}

	function openViewForm(selectId) {

		showData(selectId);
		
		openWin('查看付款', '');
		
		$("#contractPaymentId").attr("disabled", "true");
		$("#paymentRemark").attr("disabled", "true");
		$("#paymentAmount").attr("disabled", "true");
		$("#invoice").attr("disabled", "true");
		$("#invoiceAmount").attr("disabled", "true");
		$("#paymentTime").attr("disabled", "true");
		$("#requestTime").attr("disabled", "true");
		$("#handleUserId").attr("disabled", "true");
		$("#remark").attr("disabled", "true");
		
		$("#btn-save").css("display", "none"); 
	}

	var cpList;
	function getContractPayment(contractId, cpId) {
		var urlStr = "payment_getContractPaymentJsonAction.action?query_contractId="
				+ contractId;

		$.ajax({
			url : urlStr,
			async : false,
			dataType : "json",
			success : function(data) {
				if (data != null) {
					cpList = $(data.contractPaymentJson);

					$("#contractPaymentId").empty();
					cpList
							.each(function(i) {
								var option = "<option value="
										+ cpList.get(i).intId + ">"
										+ cpList.get(i).paymentName
										+ "</option>";
								$("#contractPaymentId").append(option);
							});

					if (cpId != null) {
						$("#contractPaymentId").val(cpId);
					}
					paymentChange($("#contractPaymentId")[0]);
				}
			}
		});
	}

	function paymentChange(obj) {
		var selectId = $(obj).val();
		cpList.each(function(i) {
			if (selectId == cpList.get(i).intId) {
				if ($.trim(cpList.get(i).paymentRemark) != "") {
					$(obj).next("#paymentRemark").val(
							$.trim(cpList.get(i).paymentRemark));
					$(obj).next("#paymentRemark").css("display", "");
				} else {
					$(obj).next("#paymentRemark").css("display", "none");
				}
			}
		});
	}
	
	function winInit() {
		$("#contractPaymentId").removeAttr("disabled");
		$("#paymentRemark").removeAttr("disabled");
		$("#paymentAmount").removeAttr("disabled");
		$("#invoice").removeAttr("disabled");
		$("#invoiceAmount").removeAttr("disabled");
		$("#paymentTime").removeAttr("disabled");
		$("#requestTime").removeAttr("disabled");
		$("#handleUserId").removeAttr("disabled");
		$("#remark").removeAttr("disabled");
		
		$('#btn-save').css('display', '');
	}
</script>
